﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using BusinessLayer;
using BoletoElectronicoDesktop.AbmTarjetas;

namespace BoletoElectronicoDesktop.AbmClientes
{


    public partial class BuscadorCliente : FormListDelete
    {

        public BuscadorCliente(Common.Util.Funcionalidad Use, int motivo)
            : base(Use)
        {
            InitializeComponent();
            Motivo = motivo;
            buscar();
        }
    
       
        public int Motivo;
        private int selectedId;
        public int SelectedId
        {
            get
            {
                try
                {

                    if ((dsBoleto.ClienteRow)((DataRowView)(dgwClientes.CurrentRow.DataBoundItem)).Row != null)
                    {
                        selectedId = ((dsBoleto.ClienteRow)((DataRowView)(dgwClientes.CurrentRow.DataBoundItem)).Row).IdCliente;
                    }
                }
                catch (Exception)
                {
                    selectedId = 0;
                }
                return selectedId;
            }
            set
            { selectedId = value; }
        }
        private void BuscadorCliente_Load(object sender, EventArgs e)
        {
            CargarTipoDNI();
        }
        private void CargarTipoDNI()
        {
            cbTipoDNI.DataSource = BizTipoDNI.GetTipoDNIs().TipoDNI;
            cbTipoDNI.DisplayMember = "Descripcion";
            cbTipoDNI.ValueMember = "IdTipoDNI";

            cbTipoDNI.SelectedItem = null;
        }
        private void btBuscar_Click(object sender, EventArgs e)
        {
            buscar();
        }

        public void buscar()
        {
            int dni = 0;
            if (!String.IsNullOrEmpty(txtDNI.Text))
                int.TryParse(txtDNI.Text, out dni);

            int numero = 0;
            if (!String.IsNullOrEmpty(txtNumero.Text))
                int.TryParse(txtNumero.Text, out numero);

            int piso = 0;
            if (!String.IsNullOrEmpty(txtPiso.Text))
                int.TryParse(txtPiso.Text, out piso);

            int idTipoDNI = 0;
            if (cbTipoDNI.SelectedItem != null)
                idTipoDNI = int.Parse(cbTipoDNI.SelectedValue.ToString());


            dsBoleto ds = BizCliente.getClientes(txtNombre.Text, txtApellido.Text, idTipoDNI, dni, 0, txtMail.Text, txtTelefono.Text, txtCalle.Text, numero, piso, txtDepto.Text); 
            if (ds.Cliente.Count != 0)
            {
                dgwClientes.DataSource = ds.Cliente;
            }
            else
            {
                MessageBox.Show("No existen registros para el criterio de busqueda ingresado.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btSeleccionar_Click(object sender, EventArgs e)
        {
            if(Motivo == 1) //form list
            {
                try
                {
                    FormPrincipal form = ((FormPrincipal)this.MdiParent);
                    if (form.MdiChildren.Any(f => f is FormTarjetasList))
                    {
                        FormTarjetasList frm = (FormTarjetasList)form.MdiChildren.First(f => f is FormTarjetasList);
                        frm.Cliente = (dsBoleto.ClienteRow)((DataRowView)(dgwClientes.CurrentRow.DataBoundItem)).Row;
                        frm.setCliente();
                        Close();
                    }
                }
                catch (ApplicationException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                try
                {
                    FormPrincipal form = ((FormPrincipal)this.MdiParent);
                    if (form.MdiChildren.Any(f => f is FormTarjetasList))
                    {
                        FormAbmTarjetas frm = (FormAbmTarjetas)form.MdiChildren.First(f => f is FormAbmTarjetas);
                        frm.Cliente = (dsBoleto.ClienteRow)((DataRowView)(dgwClientes.CurrentRow.DataBoundItem)).Row;
                        frm.setCliente();
                        Close();
                    }
                }
                catch (ApplicationException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

      


        private void btCancelar_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
